package com.ruoyi.project.wx.api;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.wx.domain.CmsLinkDO;
import com.ruoyi.project.wx.domain.CmsNoticeSysDO;
import com.ruoyi.project.wx.domain.CmsSlideDO;
import com.ruoyi.project.wx.domain.TreatyDO;
import com.ruoyi.project.wx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/6/17.
 */
@Controller
@RequestMapping("/api/cms")
public class ApiCmsController {

    @Autowired
    private CmsSlideService cmsSlideService;
    @Autowired
    private GoodsSpikeService goodsSpikeService;
    @Autowired
    private GoodsNineService goodsNineService;
    @Autowired
    private GoodsBargainService bargainService;
    @Autowired
    private CmsNoticeSysService cmsNoticeSysService;
    @Autowired
    private TreatyService treatyService;
    @Autowired
    private CmsLinkService cmsLinkService;

    /***
     * 获取连接
     * @param params
     * @return
     */
    @PostMapping( "/getLinkUrl")
    @ResponseBody
    public R getLinkUrl(@RequestBody Map<String, Object> params){
        if (!params.containsKey("status")|| StringUtils.isBlank(params.get("status").toString())
        ){
            return R.error("参数错误");
        }
        try{
            CmsLinkDO cmsLinkDO = cmsLinkService.getLinkUrl(params);
            return R.ok().put("data",cmsLinkDO);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取用户协议及隐私政策
     * @param params
     * @return
     */
    @PostMapping( "/getTreaty")
    @ResponseBody
    public R getTreaty(@RequestBody Map<String, Object> params){
        if (!params.containsKey("status")|| StringUtils.isBlank(params.get("status").toString())
        ){
            return R.error("参数错误");
        }
        try{
            TreatyDO treatyDO = treatyService.getTreaty(params);
            return R.ok().put("data",treatyDO);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 首页轮播图列表
     * 图片类型：0：首页轮播图，1：首页广告图，4：砍价页面广告图，5：秒杀页面广告图, 6：推广图
     */
    @PostMapping( "/slide/list")
    @ResponseBody
    public R slideList(@RequestBody Map<String, Object> params){
        if (!params.containsKey("type")|| StringUtils.isBlank(params.get("type").toString())
            ){
            return R.error("参数错误");
        }
        try{
            List<CmsSlideDO> list = cmsSlideService.queryAll(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 首页9.9专区展示
     */
    @GetMapping( "/nine")
    @ResponseBody
    public R nine(){
        try{
            Map<String,Object> map = goodsNineService.nine();
            return R.ok().put("data",map);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /*
   * 首页砍价专区展示
   * */
    @ResponseBody
    @GetMapping("/bargain")
    public R bargain() {
        try{
            Map<String,Object> map = bargainService.bargain();
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /*
   * 我的消息（公告）列表
   * */
    @PostMapping( "/noticeSys/list")
    @ResponseBody
    public R noticeSysList(@RequestBody Map<String, Object> params){
        if (!params.containsKey("pageNumber")||!params.containsKey("pageSize")
                ||!params.containsKey("status")){
            return R.error("参数异常");
        }
        try{
            PageInfo<CmsNoticeSysDO> list = cmsNoticeSysService.queryAll(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 系统公告详情
     */
    @PostMapping( "/noticeSys/info")
    @ResponseBody
    public R noticeSysInfo(@RequestBody Map<String, Object> params){
        if (!params.containsKey("noticeSysId")){
            return R.error("参数异常");
        }
        try{
            CmsNoticeSysDO cmsNoticeSysDO = cmsNoticeSysService.get(params.get("noticeSysId").toString());
            return R.ok().put("data",cmsNoticeSysDO);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }














}
